package com.bgba.oer.service;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;

import com.flashline.registry.openapi.base.APIValidationException;
import com.flashline.registry.openapi.base.OpenAPIException;
import com.flashline.registry.openapi.entity.AuthToken;
import com.flashline.registry.openapi.entity.Asset;
import com.flashline.registry.openapi.entity.Extraction;
import com.flashline.registry.openapi.entity.Project;
import com.flashline.registry.openapi.entity.ProjectEntities;
import com.flashline.registry.openapi.entity.RegistryUser;
import com.flashline.registry.openapi.query.ProjectCriteria;
import com.flashline.registry.openapi.service.v300.FlashlineRegistry;
import
com.flashline.registry.openapi.service.v300.FlashlineRegistryServiceLocator;
public class ProjectService {
	public static void main(String pArgs[]) throws ServiceException, ParserConfigurationException, SAXException, IOException  {
		try { 

			System.out.println("======================== ");
			String urlConnectionLogin = "http://desasoa01/oer/services/FlashlineRegistry?WSDL";
			String username = " L0269239";
			String password = "finde2011";
			/*
			String urlConnectionLogin = "http://172.17.1.10:8101/oer/services/FlashlineRegistry?WSDL";
			String username = " admin";
			String password = "admin123";
			 */
			///////////////////////////////////////////////////////////
			// Connect to Oracle Enterprise Repository
			///////////////////////////////////////////////////////////
			URL lURL = null;
			lURL = new URL(urlConnectionLogin);
			FlashlineRegistry repository = new FlashlineRegistryServiceLocator().getFlashlineRegistry(lURL);

			///////////////////////////////////
			// Login to OER
			///////////////////////////////////
			AuthToken authToken = repository.authTokenCreate(username, password);

			String projectNameCriteria = "Project";
			searchProjectByName(repository, authToken, projectNameCriteria);

			Project myProject = null;
			ProjectEntities myProjectEntities = null;
			addProject(repository, authToken, myProject, 99);

		} catch (Exception lEx) {
			lEx.printStackTrace();
		}
	}

	public static void addProject(FlashlineRegistry repository, AuthToken authToken, Project myProject, long projectLeaderId) throws RemoteException, APIValidationException, OpenAPIException {
		
		ProjectEntities myProjectEntities;
		String[] lLeaderIds = null;
		//-----------------------------------------------------------------------------------
		//myProject = new Project();
		myProjectEntities = new ProjectEntities();
		//-----------------------------------------------------------------------------------
		// set the name of project
		//myProject.setName("NEW_PROJECT_NAME");
		//myProject.setDisplayName("The New Proyect Name");
		myProject.setStatusString("Open");
		
		//-----------------------------------------------------------------------------------
		// set the name of the project's department
		myProject.setDepartmentID(50000); 
		//---  myProject.setDepartmentName("Default Department");
		// a department with id 50000 must
		// already exist
		//-----------------------------------------------------------------------------------
		// set the userids of the project leaders
		if (projectLeaderId > 0){
			lLeaderIds = new String[] { String.valueOf(projectLeaderId) };
		} else {
			// si en SharePoint no existe el Responsable, en el OER asignamos el usuario Admin del OER
			// 99 es el ID del usuario admin del oer
			lLeaderIds = new String[] { "99" }; 
		}
		myProjectEntities.setLeaderIDs(lLeaderIds);
		
		//-----------------------------------------------------------------------------------
		Project newProject = repository.projectCreate(authToken, myProject, myProjectEntities);
		System.out.println("===================== newProject.getID="+newProject.getID());
	}

	public static Project searchProjectByName(FlashlineRegistry repository, AuthToken authToken, String projectNameCriteria) throws RemoteException, OpenAPIException {
		// Criteria for Projects
		ProjectCriteria projectCriteria = new ProjectCriteria();
		projectCriteria.setNameCriteria(projectNameCriteria);
		// Read a project
		Project[] projects = repository.projectQuery(authToken, projectCriteria);

		Project myProject = null;
		
		if (projects.length == 1) {
			try {
				myProject = (Project) projects[0];
				System.out.println("========================= myProject.getID="+myProject.getID());
				System.out.println("========================= myProject.getName="+myProject.getName());
				System.out.println("========================= myProject.getDisplayName="+myProject.getDisplayName());
				System.out.println("========================= myProject.getDepartmentID="+myProject.getDepartmentID());
				System.out.println("========================= myProject.getDepartmentName="+myProject.getDepartmentName());
				System.out.println("========================= myProject.getStatusString="+myProject.getStatusString());

				Extraction[] lExtractions = repository.projectReadExtractions(authToken, myProject);
				Asset[] lAssets = repository.projectReadProducedAssets(authToken, myProject);
				Project[] childProjects = repository.projectReadChildProjects(authToken, myProject);
				Project[] parentProjects = repository.projectReadParentProjects(authToken, myProject);
				RegistryUser[] members = repository.projectReadMembers(authToken, myProject);
				RegistryUser[] leaders = repository.projectReadLeaders(authToken, myProject);
			} catch (OpenAPIException ex) {
				System.err.println("####################################### ERROR!! - "+ex.getMessage());
				ex.printStackTrace();
			}
		} else if (projects.length > 1) {
			System.out.println("####################################### WARN - Found more than 1 Projects, resultSet="+projects.length + ", projectNameCriteria="+projectNameCriteria);
		} else {
			System.out.println("####################################### WARN - NOT Found Projects projectNameCriteria="+projectNameCriteria);
		}
		
		return myProject;
	}
}
